package com.ido.otaclient.realtk;

import android.content.Context;

import com.ido.otaclient.base.BasePreference;
import com.ido.otaclient.env.AppEnv;

/**
 * Copyright (c) 2019 深圳市爱都科技有限公司. All rights reserved.
 *
 * @Author: xyb
 * @CreateDate: 2020-03-07 10:48
 * @description
 */
public class RealtkConfigPerfence extends BasePreference {
    private static final String NAME = "realtk_preference";

    private static final String TASK_NAME = "taskName";
    private static final String UPLOAD_PLATFORM = "uploadPlatform";
    private static final String FIREWARE_NORMAL_NAME = "firewareNormalName";
    private static final String FIREWARE_NORMAL_VERSION = "firewareNormalVersion";
    private static final String FIREWARE_NORMAL_PATH = "filewareNormalPath";
    private static final String DEVICE_ID = "deviceId";
    /**
     * 升级配置
     */
    private RealtkConfig mRealtkConfig;

    private static volatile RealtkConfigPerfence instance;

    private static Context getContext() {
        return AppEnv.instance().getContext();
    }

    public static RealtkConfigPerfence getInstance() {
        if (instance == null) {
            synchronized (RealtkConfigPerfence.class) {
                if (instance == null) {
                    instance = new RealtkConfigPerfence();
                }
            }
        }
        return instance;
    }

    /**
     * 保存配置在本地
     * @param realtkConfig
     */
    public void saveRealtkConfig(RealtkConfig realtkConfig){
        if(realtkConfig == null){
            realtkConfig = new RealtkConfig();
        }
        saveString(getContext(),NAME,TASK_NAME,realtkConfig.getTaskName());
        saveString(getContext(),NAME,UPLOAD_PLATFORM,realtkConfig.getUploadPlatform());
        saveString(getContext(),NAME,FIREWARE_NORMAL_NAME,realtkConfig.getFirewareNormalName());
        saveString(getContext(),NAME,FIREWARE_NORMAL_VERSION,realtkConfig.getFirewareNormalVersion());
        saveString(getContext(),NAME,FIREWARE_NORMAL_PATH,realtkConfig.getFirewareWarePath());
        saveString(getContext(),NAME,DEVICE_ID,realtkConfig.getDeviceId());
        mRealtkConfig = realtkConfig;
    }

    /**
     * 从本地取出数据
     * @return
     */
    public RealtkConfig getRealtkConfig(){
        mRealtkConfig = new RealtkConfig();
        mRealtkConfig.setTaskName(getString(getContext(),NAME,TASK_NAME));
        mRealtkConfig.setUploadPlatform(getString(getContext(),NAME,UPLOAD_PLATFORM));
        mRealtkConfig.setFirewareNormalName(getString(getContext(),NAME,FIREWARE_NORMAL_NAME));
        mRealtkConfig.setFirewareNormalVersion(getString(getContext(),NAME,FIREWARE_NORMAL_VERSION));
        mRealtkConfig.setFirewareWarePath(getString(getContext(),NAME,FIREWARE_NORMAL_PATH));
        mRealtkConfig.setDeviceId(getString(getContext(),NAME,DEVICE_ID));
        return mRealtkConfig;
    }

}
